package com.zklh.book.system.rest;

import com.zklh.book.system.common.model.RestResponse;
import com.zklh.book.system.model.User;
import com.zklh.book.system.service.UserService;
import com.zklh.book.system.service.impl.UserServiceImpl;
import com.zklh.book.system.util.BeanFactory;

/**
 * 用于处理用户相关的请求
 *
 * @author 坐看落花
 * @version 1.0
 * @date 2021/3/20 19:26
 */
public class UserController {

    private UserService userService = BeanFactory.getBean(UserServiceImpl.class);

    /**
     * 登录请求, 返回登录凭证 token
     * @param account
     * @param password
     * @return
     */
    public RestResponse<User> login (String account, String password) {
        String token = userService.login(account, password);
        if (token != null) {
            User user = userService.get(account);
            user.setToken(token);
            return RestResponse.success(user);
        } else {
            return RestResponse.error(501, "账号或者密码错误!");
        }

    }

    /**
     * 注销
     * @param token
     * @return
     */
    public RestResponse<String> logout(String token) {
        userService.logout(token);
        return RestResponse.success("退出成功!");
    }


}
